<?php

class ChannelAction extends CommonAction {

    // 查询数据
    public function index() {
        $this->display();
    }
	public function indexjson() {
        $Channel = M("Channel");
		$channelSelect = $this->getChannelSelect( 0);
		$channelSelectHtml['rows'] = $channelSelect ? $channelSelect : array() ;
		echo json_encode($channelSelectHtml);
    }
	public function treejson () {
		$id = intval( $_POST['form_pid'] ) ? intval( $_POST['form_pid'] )  : 0;
		echo json_encode( $this->channeltree( ) );
	}
    // 写入数据
    public function add() {
        $Channel = D("Channel");
        if ($vo = $Channel->create()) {
            $list = $Channel->add();
            if ($list !== false) {
                $this->success('数据保存成功！');
            } else {
                $this->error('数据写入错误！');
            }
        } else {
            $this->error($Channel->getError());
        }
    }

    // 更新数据
    public function edit() {
        $Channel = D("Channel");
        if ($vo = $Channel->create()) {
            $list = $Channel->save();
            if ($list !== false) {
                $this->success('数据更新成功！');
            } else {
                $this->error("没有更新任何数据!");
            }
        } else {
            $this->error($Channel->getError());
        }
    }

    // 删除数据
    public function remove() {
		$id = intval( $_POST['form_id'] );
        if (!empty($id)) {
            $Channel = M("Channel");
			$vo = $Channel->where("id='{$id}'")->find();
            if ($vo) {
				$Channel->where("pid='$id'")->delete();
				$result = $Channel->delete($id);
				if (false !== $result) {
					$this->success('删除成功！');
				} else {
					$this->error('删除出错！');
				}
			}
        } else {
            $this->error('ID错误！');
        }
    }

	function channeltree($id=0) {
		$Channel =  D('Channel');
		//$id = intval( $_POST['form_pid'] ) ? intval( $_POST['form_pid'] )  : 0;
		$power = array();
		$channels = $Channel->where(" pid='{$id}' AND status='0' ")->order('sort')->select();
		if($id==0){
			$power[0]['id'] = 0;
			$power[0]['text'] = '顶级频道';
			$i=1;
		}else{
			$i=0;
		}
		foreach( $channels as $row ) {
			//@todo 频道菜单关联用户权限 ？？
			$power[$i]['id'] = $row['id'];
			$power[$i]['text'] = $row['channelname'];
			$power[$i]['children'] = $this->channeltree( $row['id'] );
			$channel_sub = $Channel->where( "pid='{$row['id']}' AND status='0' ")->count();
			if( $channel_sub ){
				$power[$i]['state'] = 'closed';
			}else{
				$power[$i]['state'] = 'open';
			}
			$i++;
		}
		return $power;
	}
	
	function getChannelSelect($id = 0,$suojin = ''){
		static $i,$channelSelect;
		$Channel =  D('Channel');
		if($id==0){
			$pname = '顶级分类';
		}else{
			$pnameRet = $Channel->where("id='{$id}' AND status='0'")->field('channelname')->find();
			$pname = $pnameRet['channelname'];
		}
		$channels = $Channel->where(" pid='{$id}' AND status='0' ")->order('sort')->select();
		foreach( $channels as $row ) {
			if(!isset($i)){
				$i = 0;
			}else{
				$i++;
			}
			$channelSelect[$i]['table_id']= $row['id'];
			$channelSelect[$i]['table_pid'] = $row['pid'];
			$channelSelect[$i]['table_pname'] = $pname;
			$channelSelect[$i]['table_sort'] = $row['sort'];
			$channelSelect[$i]['table_name'] = $suojin.$row['channelname'];
			$channelSelect[$i]['table_url'] = $row['channelurl'];
			$channelSelect[$i]['table_info'] = $row['info'];
			$channelSelect[$i]['table_isshow'] = $row['isshow'];
			$channel_sub = $Channel->where( "pid='{$row['id']}' AND status='0' ")->count();
			if( $channel_sub ){
				$this->getChannelSelect($row['id'],$suojin.'──');
			}
		}
		return $channelSelect;
	}
	
	 

		
		
		
		
		
}
?>